{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d7bdcb38",
   "metadata": {},
   "source": [
    "如果要让内部属性不被外部访问，可以把属性的名称前加上两个下划线。python中，实例的变量名如果以下划线开头\n",
    "就变成了一个私有变量，只有内部可以访问，外部不能访问。如下："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "3e8362ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Student(object):\n",
    "    def __init__(self, name, score):\n",
    "        self.__name = name\n",
    "        self.__score = score\n",
    "    def print_score(self):\n",
    "        print('%s: %s' % (self.__name, self.__score))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "b4a2ce80",
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Student' object has no attribute '__name'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp/ipykernel_31748/2493582476.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[0mbart\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mStudent\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'Bart'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m59\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mbart\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m__name\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m: 'Student' object has no attribute '__name'"
     ]
    }
   ],
   "source": [
    "bart = Student('Bart', 59)\n",
    "bart.__name"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56c94f23",
   "metadata": {},
   "source": [
    "改完后，对于外部代码来说，已经无法访问实例变量.__name"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8876b975",
   "metadata": {},
   "source": [
    "如果外部代码要获取name或者score怎么办？可以增加get_name这样的方法获得："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2986b67c",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Student(object):\n",
    "    def __init__(self, name, score):\n",
    "        self.__name = name\n",
    "        self.__score = score\n",
    "    def print_score(self):\n",
    "        print('%s: %s' % (self.__name, self.__score))\n",
    "    def get_name(self):\n",
    "        return self.__name\n",
    "    def get_score(self):\n",
    "        return self.__score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "9b9d4e29",
   "metadata": {},
   "outputs": [],
   "source": [
    "bart = Student('Bob', 99)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "8541c034",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Bob'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bart.get_name()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5e687b60",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
